from django.shortcuts import render
from rest_framework import viewsets
from rest_framework import mixins
from rest_framework.permissions import IsAuthenticated
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.authentication import SessionAuthentication
from rest_framework.response import Response
from django.db.models import Q
from django.contrib.auth import get_user_model
from .models import UserImage, UserConcernlove
from utils.permissions import IsOwnerOrReadOnly, IsOwnerOrReadOnlyconcert
from .serializers import (UserimageSerializer, UserFavSerializer, UserFavDetailSerializer,
                          FavUserSerializer,MatchUserSerializer)

# Create your views here.
User = get_user_model()
class UseImageViewset(mixins.ListModelMixin, mixins.DestroyModelMixin, mixins.CreateModelMixin,
                            viewsets.GenericViewSet):
    """
    list:
        获取用户头像
    create:
        添加用户头像
    delete:
        删除用户功能
    """
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    serializer_class = UserimageSerializer

    def get_queryset(self):
        return UserImage.objects.filter(user=self.request.user)

class UsefavViewset(mixins.ListModelMixin, mixins.DestroyModelMixin, mixins.CreateModelMixin,
                            viewsets.GenericViewSet):
    """
        list:
            获取用户关注的对象
        retrieve:
            判断某个人用户是否喜欢
        delete:
            取消用户喜欢
    """

    permission_classes = (IsAuthenticated, IsOwnerOrReadOnlyconcert)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    queryset = UserConcernlove.objects.all()
    #serializer_class = UserFavSerializer
    lookup_field = "concertuser_id"

    def get_queryset(self):
        return UserConcernlove.objects.filter(userConcern=self.request.user)

    def get_serializer_class(self):
        if self.action == "list":
            return UserFavDetailSerializer
        elif self.action == "create":

            return UserFavSerializer

        return UserFavSerializer



class FavuserViewset(mixins.ListModelMixin, viewsets.GenericViewSet):
    '''
    喜欢当前用户的接口
    '''
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnlyconcert)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    queryset = UserConcernlove.objects.all()
    serializer_class = FavUserSerializer
    lookup_field = "userConcern_id"

    def get_queryset(self):
        return UserConcernlove.objects.filter(concertuser=self.request.user)


class MatchFavUserViewSet(mixins.ListModelMixin,viewsets.GenericViewSet):
    '''
    list:
        获取相互喜欢的对象
    delete:
        取消用户喜欢
    '''
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnlyconcert)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    serializer_class = MatchUserSerializer
    lookup_field = "userConcern_id"

    def get_queryset(self):
        return UserConcernlove.objects.filter(Q(concertuser=self.request.user) & Q(matchsucess='0') )








